package com.example.roll;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView tView, totaltv, hearthTV;
	private Button btn, clrButton;
	private String str = "";
	private int total = 0;
	private int COLORS[] = new int[] { Color.RED, Color.MAGENTA, Color.BLACK,
			Color.GREEN, Color.GRAY, Color.CYAN };

	// ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.GRAY);
	SharedPreferences spPreferences;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1: {
				double m = Math.random() * 100;
				int mm = (int) m;
				hearthTV.setText("" + mm);
			}
				break;
			case 2: {
				hearthTV.setText("" + msg.arg1);
			}
				break;
			case 0: {
				btn.setVisibility(View.VISIBLE);
				hearthTV.setVisibility(View.GONE);
				setm();
			}
				break;
			default:
				break;
			}
		};
	};

	private void setm() {
		totaltv.setText("总共的人数为： " + total);
		SpannableStringBuilder builder = new SpannableStringBuilder(str);
		// ForegroundColorSpan 为文字前景色，BackgroundColorSpan为文字背景色
		int laststart = 0;
		int len = 0;
		int sp = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) != '\n') {
				len++;
				Log.i("allan", "! =  len " + len);
			} else {
				ForegroundColorSpan spans = new ForegroundColorSpan(
						COLORS[(sp++) % COLORS.length]);
				builder.setSpan(spans, laststart, laststart + len,
						Spanned.SPAN_INTERMEDIATE);
				tView.setText(builder);
				Log.i("allan",
						"str " + str.substring(laststart, laststart + len));
				Log.i("allan", "last1 " + laststart);
				laststart = laststart + len + 1;
				Log.i("allan", "last2 " + laststart);
				len = 0;
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		spPreferences = getSharedPreferences("mine", Context.MODE_PRIVATE);
		str = spPreferences.getString("save", "");
		total = spPreferences.getInt("total", 0);
		tView = (TextView) findViewById(R.id.textView1);
		totaltv = (TextView) findViewById(R.id.totaltv);
		hearthTV = (TextView) findViewById(R.id.hearthShow);
		btn = (Button) findViewById(R.id.btn);
		clrButton = (Button) findViewById(R.id.clear);
		clrButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						MainActivity.this);
				builder.setIcon(R.drawable.ic_launcher).setNegativeButton("OK",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								spPreferences.edit().putString("save", "").commit();
								spPreferences.edit().putInt("total", 0).commit();
								tView.setText("");
								totaltv.setText("总共的人数： 0");
								total = 0;
								str = "";
							}
						});
				builder.setPositiveButton("cancel", null);
				builder.create().show();
			}
		});
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				total++;
				spPreferences.edit().putString("save", str).commit();
				spPreferences.edit().putInt("total", total).commit();
				btn.setVisibility(View.GONE);
				hearthTV.setVisibility(View.VISIBLE);
				new Thread(new Runnable() {

					@Override
					public void run() {
						for (int i = 0; i < 4; i++) {
							try {
								Thread.sleep(150);
							} catch (InterruptedException e) {
							}
							Message.obtain(mHandler, 1).sendToTarget();
						}
						for (int i = 0; i < 5; i++) {
							try {
								Thread.sleep(120);
							} catch (InterruptedException e) {
							}
							Message.obtain(mHandler, 1).sendToTarget();
						}
						for (int i = 0; i < 60; i++) {
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {
							}
							Message.obtain(mHandler, 1).sendToTarget();
						}

						for (int i = 0; i < 7; i++) {
							try {
								Thread.sleep(100);
							} catch (InterruptedException e) {
							}
							Message.obtain(mHandler, 1).sendToTarget();
						}
						int saved = 0;
						for (int i = 0; i < 4; i++) {
							try {
								Thread.sleep(290);
							} catch (InterruptedException e) {
							}
							
							double m = Math.random() * 100;
							saved =  (int) m;
							Message msg = mHandler.obtainMessage();
							msg.what = 2;
							msg.arg1 = saved;
							Message.obtain(msg).sendToTarget();
						}
						str += saved + "\n";
						try {
							Thread.sleep(300);
						} catch (InterruptedException e) {
						}
						Message.obtain(mHandler, 0).sendToTarget();
					}
				}).start();
				// setm();
			}
		});
		setm();
	}
}
